﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace YXShop.Web.admin.systeminfo
{
    public partial class mouldtype_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!this.Page.IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("010005001");
                BLL.SystemInfo.MouldType bll = new YXShop.BLL.SystemInfo.MouldType();
                string action = ChangeHope.WebPage.PageRequest.GetQueryString("action");
                int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
                if(action.Equals("del") && id>0)
                {
                    if (YXShop.Common.PromptInfo.Message("010005003") != "ok")
                    {
                        if (Convert.ToInt32(MouldInfoCount(id.ToString())) > 0)
                        {
                            ChangeHope.WebPage.Script.AlertAndGoBack("请删除该类型下的模型,再删除该类型!");
                            Response.End();
                        }
                        else
                        {
                            bll.Delete(id);
                        }
                    }
                    else
                    {
                        Response.Write("no");
                    }
                }
                string isStoreLabel = ChangeHope.WebPage.PageRequest.GetQueryString("w_d_isstoremould");
                if (isStoreLabel != "")
                {
                    this.Link.NavigateUrl = "mouldtype_edit.aspx?IsStoreLabel=" + isStoreLabel;
                }
                this.Literal1.Text = this.ListInfo();
                bll = null;
            }
        }

        #region 列表信息
        protected string ListInfo()
        {
            string isStoreLabel = ChangeHope.WebPage.PageRequest.GetQueryString("w_d_isstoremould");
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.SystemInfo.MouldType data = new YXShop.BLL.SystemInfo.MouldType();
            ChangeHope.DataBase.DataByPage dataPage = data.GetList();
            //第一步先添加表头
            table.AddHeadCol("5%", "序号");
            table.AddHeadCol("20%", "模型类别名称");
            table.AddHeadCol("40%", "模型类别描述");
            table.AddHeadCol("20%", "添加时间");
            table.AddHeadCol("15%", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol(No);
                    table.AddCol(string.Format("<a href='mouldinfo_list.aspx?w_d_MouldType={2}&isStoreLabel={3}'>{0}</a><span style='color:red'>({1})</span>", dataPage.DataReader["name"].ToString(), MouldInfoCount(dataPage.DataReader["id"].ToString()), dataPage.DataReader["id"].ToString(), isStoreLabel));
                    table.AddCol(dataPage.DataReader["description"].ToString());
                    table.AddCol(Convert.ToDateTime(dataPage.DataReader["addtime"].ToString()).ToString("yyyy-MM-dd"));
                    table.AddCol(string.Format("<a href=mouldtype_edit.aspx?id={0}>编辑</a> <a href='?w_d_isstoremould={2}&action=del&id={0}' onclick='return confirm(\"是否删除该项\")'>删除</a>", dataPage.DataReader["id"].ToString(), dataPage.DataReader["id"].ToString(), isStoreLabel));

                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }

        private string MouldInfoCount(string MouldTypeId)
        {
            string reStr = "0";
            YXShop.BLL.SystemInfo.MouldInfo bll = new YXShop.BLL.SystemInfo.MouldInfo();
            DataTable dt = bll.GetTypeList(MouldTypeId);
            if (dt.Rows.Count > 0)
            {
                reStr = dt.Rows.Count.ToString();
            }
            return reStr;
        }
        #endregion
    }
}
